package com.lu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lu.constants.BlogStatusConstants;
import com.lu.constants.ConfigConstants;
import com.lu.constants.HttpStatusEnum;
import com.lu.dto.AjaxPutPage;
import com.lu.dto.AjaxResultPage;
import com.lu.dto.Result;
import com.lu.pojo.BlogInfo;
import com.lu.pojo.BlogTag;
import com.lu.pojo.BlogTagRelation;
import com.lu.service.BlogInfoService;
import com.lu.service.BlogTagRelationService;
import com.lu.service.BlogTagService;
import com.lu.utils.DateUtils;
import com.lu.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author xiaolu
 * @since 2020-05-31
 */
@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private BlogTagService blogTagService;

    @Autowired
    private BlogTagRelationService blogTagRelationService;

    @Autowired
    private BlogInfoService blogInfoService;

    // 返回未删除状态下的所有标签
    @ResponseBody
    @GetMapping("/v1/tags/list")
    public Result<List<BlogTag>> list(){
        QueryWrapper<BlogTag> queryWrapper = new QueryWrapper<BlogTag>();
        queryWrapper.lambda().eq(BlogTag::getIsDeleted, BlogStatusConstants.ZERO);
        List<BlogTag> list = blogTagService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)){
            ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);

        }
        return ResultGenerator.getResultByHttp(HttpStatusEnum.OK,list);
    }

    // 跳转到标签分类页面
    @GetMapping("/v1/tags")
    public String gotoTag(){
        return "admin/tag-list";
    }

    // 标签分页
    @ResponseBody
    @GetMapping("/v1/tags/paging")
    public AjaxResultPage<BlogTag> getCategoryList(AjaxPutPage<BlogTag> ajaxPutPage, BlogTag condition){

        QueryWrapper<BlogTag> queryWrapper = new QueryWrapper<>(condition);
        queryWrapper.lambda()
                .ne(BlogTag::getTagId,1);
        Page<BlogTag> page = ajaxPutPage.putPageToPage();
        blogTagService.page(page,queryWrapper);
        AjaxResultPage<BlogTag> result = new AjaxResultPage<>();
        result.setData(page.getRecords());
        result.setCount(page.getTotal());
        return result;
    }

    // 修改标签状态
    @PostMapping("/v1/tags/isDel")
    @ResponseBody
    public Result<String> isDel(BlogTag blogTag){

        // 更新标签状态
        boolean flag = blogTagService.updateById(blogTag);
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
        }else {
            return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    // 添加标签
    @PostMapping("/v1/tags/add")
    @ResponseBody
    public Result<String> add(BlogTag blogTag){

        // 设置创建时间
        blogTag.setCreateTime(DateUtils.getLocalCurrentDate());
        // 添加新标签
        boolean flag = blogTagService.save(blogTag);
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKKK);
        }else {
            return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    // 清除标签
    @PostMapping("v1/tags/clear")
    @ResponseBody
    public Result<String> clear(Integer tagId){

        QueryWrapper<BlogTagRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogTagRelation::getTagId,tagId);
        List<BlogTagRelation> tagRelationList = blogTagRelationService.list(queryWrapper);
        // 批量更新的BlogInfo信息
        List<BlogInfo> infoList = tagRelationList.stream()
                .map(tagRelation -> new BlogInfo()
                        .setBlogId(tagRelation.getBlogId())
                        .setBlogTags(ConfigConstants.DEFAULT_TAG.getConfigName())).collect(Collectors.toList());
        List<Long> blogIds = infoList.stream().map(BlogInfo::getBlogId).collect(Collectors.toList());
        // 批量更新的tagRelation信息
        List<BlogTagRelation> tagRelations = tagRelationList.stream()
                .map(tagRelation -> new BlogTagRelation()
                        .setBlogId(tagRelation.getBlogId())
                        .setTagId(Integer.valueOf(ConfigConstants.DEFAULT_CATEGORY.getConfigField())))
                .collect(Collectors.toList());
        blogInfoService.updateBatchById(infoList);
        blogTagRelationService.remove(new QueryWrapper<BlogTagRelation>()
                .lambda()
                .in(BlogTagRelation::getBlogId,blogIds));
        blogTagRelationService.saveBatch(tagRelations);
        blogTagService.removeById(tagId);
        return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
    }

}

